var AetherUpload = {
    upload: function () {
            
        $.ajaxSetup({
            headers: {
                "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
            }
        });
        
            this.fileDom = this.wrapperDom.find(".file"),
            /* this.outputDom = this.wrapperDom.find(".output"),

            this.progressBarDom = this.wrapperDom.find(".progressbar"), */
            this.outputDom = this.wrapperDom.find(".progressBoxChild"),//显示出来的文字

            this.progressBarDom = this.wrapperDom.find(".progressBoxChild"),//进度条

            this.context = this.wrapperDom.find(".uploadeModeFile"),//上传的input

            this.fileStart = this.wrapperDom.next("div"),//显示准备上传
            
            this.savedPathDom = this.wrapperDom.find(".savedpath"),

            this.uploadImg = this.wrapperDom.find(".uploadMainBox"),//上传之前显示

            this.uoloadProgressBox = this.wrapperDom.find(".uoloadProgressBox"),//上传开始显示显示
            
            this.packageFileSize = this.wrapperDom.find("#packageFileSize"),//压缩包大小的dom

            this.file = this.fileDom[0].files[0],

            this.fileName = this.file.name,

            this.fileSize = this.file.size,

            this.uploadBaseName = "",

            this.uploadExt = "",

            this.chunkSize = 0,

            this.chunkCount = 0,

            this.subDir = "",

            this.savedPath = "",

            this.fileHash = "",

            this.blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,

            this.i = 0,

            this.locale,

            this.messages = this.getLocalizedMessages();
            if (this.uploadImg.length==0) {
              this.outputDom.text(this.messages.status_upload_begin);
            }
            

        if (!this.blobSlice) {

            this.outputDom.text(this.messages.error_unsupported_browser);

            return;

        }

        if (!("FileReader" in window) || !("File" in window)) {

            this.preprocess(); //浏览器不支持读取本地文件，跳过计算hash

        } else {
            
            this.calculateHash();

        }

    },

    calculateHash: function () { //计算hash
        this.outputDom.text("");
        if (this.context.attr('accept') == ".rar,.zip") {
          if (parseFloat(this.fileSize / (1000 * 1000)).toFixed(2) > 500) {
              
            this.wrapperDom.find(".file").attr("type", "button");
            this.wrapperDom.find(".file").attr("type", "file");
            $("body").append(`<div id="alert" class="alert alert-danger alert-dismissible" role="alert" style="position:fixed;top:45%;z-index:99;left:45%;">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <strong>上传失败</strong> 文件尺寸不能超过500M
</div>`);
            $("#alert").animate({ opacity: "0" }, 2000, "linear", function() {
                $("#alert").remove();
            });
            return;
          }
        } else if (this.context.attr("accept") == "image/jpeg,image/jpg,image/png") {
                 if (parseFloat(this.fileSize / (1000 * 1000)).toFixed(2) > 5) {
                     this.wrapperDom.find(".file").attr("type", "button");
                    this.wrapperDom.find(".file").attr("type", "file");
                   $("body").append(`<div id="alert" class="alert alert-danger alert-dismissible" role="alert" style="position:fixed;top:45%;z-index:99;left:45%;">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <strong>上传失败</strong> 文件尺寸不能超过5M
</div>`);
                    $("#alert").animate({ opacity: "0" }, 2000, "linear", function() {
                        $("#alert").remove();
                    });
                   return;
                 }
               }
        if(this.packageFileSize){//记录压缩包文件的大小
             this.packageFileSize.attr("fileSize", parseFloat(this.fileSize / (1000 * 1000)).toFixed(2)+"M");
        }
       
        var _this = this,

            chunkSize = 2000000,

            chunks = Math.ceil(_this.file.size / chunkSize),

            currentChunk = 0,

            spark = new SparkMD5.ArrayBuffer(),

            fileReader = new FileReader();
        fileReader.onload = function (e) {

            spark.append(e.target.result);

            ++currentChunk;

            // _this.outputDom.text(_this.messages.status_hashing + ' ' + parseInt(currentChunk / chunks * 100) + "%");
            
            if (_this.uploadImg.length==0) {//是否需要显示图片的名称
                _this.fileStart.text(_this.messages.status_hashing + " " + parseInt((currentChunk / chunks) * 100) + "%");
                
                _this.fileStart.css({
                    "background":"#ea514b",
                    "color":"#fff"
                });
               

            } 

            if (currentChunk < chunks) {

                loadNext();

            } else {

                _this.fileHash = spark.end();

                _this.preprocess();

            }
        };

        fileReader.onerror = function () {

            _this.preprocess();

        };

        function loadNext() {

            var start = currentChunk * chunkSize,

                end = start + chunkSize >= _this.file.size ? _this.file.size : start + chunkSize;

            fileReader.readAsArrayBuffer(_this.blobSlice.call(_this.file, start, end));

        }

        loadNext();

    },

    preprocess: function () { //预处理
        var _this = this;
        if (this.uploadImg.length!=0){//如果存在就显示或隐藏
            this.uploadImg.hide();
            this.uoloadProgressBox.show();
        }
        //appPath为测试与正式地址
        $.post(appPath+"aetherupload/preprocess", {

            file_name: _this.fileName,

            file_size: _this.fileSize,

            file_hash: _this.fileHash,

            locale: _this.locale,

            group: _this.group

        }, function (rst) {
            if (rst.error) {

                _this.outputDom.text(rst.error);

                return;

            }
            _this.uploadBaseName = rst.uploadBaseName;

            _this.uploadExt = rst.uploadExt;

            _this.chunkSize = rst.chunkSize;

            _this.chunkCount = Math.ceil(_this.fileSize / _this.chunkSize);

            _this.subDir = rst.subDir;
            if (rst.savedPath.length === 0) {

                // _this.outputDom.text(_this.messages.status_uploading + " 0%");//隐藏正在上传字样

                // _this.uploadChunkInterval = setInterval($.proxy(_this.uploadChunk, _this), 10000);
               /*  for(var i=0;i<_this.chunkCount;i++){
                    console.log(i)
                    _this.uploadChunkInterval = setTimeout($.proxy(_this.uploadChunk, _this), 0);
                } */
                _this.context.attr("disabled","disabled")
                _this.uploadChunkInterval = setTimeout($.proxy(_this.uploadChunk, _this), 0);//采用延时定时器

            } else {

                _this.progressBarDom.css("width", "100%");

                _this.savedPath = rst.savedPath;

                _this.savedPathDom.val(_this.savedPath);

                // _this.fileDom.attr("disabled", "disabled");

                // _this.outputDom.text(_this.messages.status_instant_completion_success);
                 _this.outputDom.text(_this.fileName);
                typeof(_this.callback) !== "undefined" ? _this.callback() : null;

            }
            _this.wrapperDom.find(".file").attr("type","button");
            _this.wrapperDom.find(".file").attr("type", "file");

        }, "json");

    },

    uploadChunk: function () {//开始上传  n
        var _this = this,
            start = this.i * this.chunkSize,
            
            end = Math.min(this.fileSize, start + this.chunkSize),

            form = new FormData();

            
             _this.fileStart.text(_this.messages.status_uploading);

             _this.fileStart.css({
               background: "#f5f5f5",
               color: "#a0a0a0"
             });

        form.append("file", this.file.slice(start, end));

        form.append("upload_ext", this.uploadExt);

        form.append("chunk_total", this.chunkCount);

        form.append("chunk_index", this.i + 1);

        form.append("upload_basename", this.uploadBaseName);

        form.append("group", this.group);

        form.append("sub_dir", this.subDir);

        form.append("locale", this.locale);
        $.ajax({
            //appPath为测试与正式地址
            // url: appPath+"api/aetherupload/uploading",
            url: appPath+"uploading",

            type: "POST",

            data: form,

            dataType: "json",

            // async: false,

            processData: false,

            contentType: false,

            success: function (rst) {
                if (rst.error) {

                    _this.outputDom.text(rst.error);

                    clearInterval(_this.uploadChunkInterval);

                    return;

                }

                var percent = parseInt((_this.i + 1) / _this.chunkCount * 100);
                var timer = setTimeout($.proxy(_this.uploadChunk, _this), 0);//每次请求有变化都会生成一个延时定时器
                if(percent==100){//如果上传成功就清空延时定时器
                    _this.context.removeAttr("disabled")
                    clearTimeout(timer)
                }
                
                _this.progressBarDom.text("已上传" + percent+"%");//显示图片上传的百分比  n

                _this.progressBarDom.css("width", (percent<50?50:percent) + "%");

                // _this.outputDom.text(_this.messages.status_uploading + " " + percent + "%");//隐藏正在上传字样

                if (_this.i + 1 === _this.chunkCount) {

                    clearInterval(_this.uploadChunkInterval);

                    _this.savedPath = rst.savedPath;

                    _this.savedPathDom.val(_this.savedPath);

                    // _this.fileDom.attr("disabled", "disabled");

                    _this.outputDom.text(_this.messages.status_upload_success);
                    typeof(_this.callback) !== "undefined" ? _this.callback() : null;

                }

                ++_this.i;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var timer = setTimeout($.proxy(_this.uploadChunk, _this), 0);//每次请求有变化都会生成一个延时定时器
                if (XMLHttpRequest.status === 0) {

                    _this.outputDom.text(_this.messages.status_retrying);

                    _this.sleep(3000);

                } else {

                    _this.outputDom.text(_this.messages.error_upload_fail);

                    clearInterval(_this.uploadChunkInterval);

                }
            }

        });

    },

    sleep: function (milliSecond) {
        
        var wakeUpTime = new Date().getTime() + milliSecond;

        while (true) {

            if (new Date().getTime() > wakeUpTime) {

                return;
            }
        }
    },

    success: function (callback) {
        this.callback = callback;
        return this;
    },

    getLocalizedMessages: function () {
        var lang = navigator.language ? navigator.language : navigator.browserLanguage;

        var locales = Object.getOwnPropertyNames(this.text);

        for (var k in locales) {

            if (lang.indexOf(locales[k]) > -1) {

                this.locale = locales[k];

                return this.text[this.locale];

            }

        }

        this.locale = "en";

        return this.text[this.locale];

    },

    text: {
        en: {
            status_upload_begin: "upload begin",
            error_unsupported_browser: "Error: unsupported browser",
            status_hashing: "hashing",
            status_instant_completion_success: "upload success (instant completion) ",
            status_uploading: "uploading",
            status_upload_success: "upload success",
            status_retrying: "network problem, retrying...",
            error_upload_fail: "Error: upload failed"
        },
        zh: {
            status_upload_begin: "开始上传",
            error_unsupported_browser: "错误：上传组件不被此浏览器支持",
            status_hashing: "准备上传",
            status_instant_completion_success: "上传成功（秒传）",
            status_uploading: "正在上传，请耐心等待...",
            status_upload_success: "上传成功",
            // status_retrying: "网络故障，正在重试……",
            status_retrying: "正在上传，请耐心等待...",
            // error_upload_fail: "错误：上传失败",
            error_upload_fail: "正在上传，请耐心等待..."
        }
    }


};

/*
 * 创建AetherUpload对象的全局方法
 * file 文件对象
 * group 分组名
 */
function aetherupload(file, group) {
    
    var newInstance = Object.create(AetherUpload);

    newInstance.wrapperDom = $(file).parents(".aetherupload-wrapper");

    newInstance.group = group;
    return newInstance;
}





